package gyn.fgsl;

import gyn.fgsl.fragments.HomeFragment;
import android.os.Bundle;
import android.view.KeyEvent;

import com.actionbarsherlock.view.MenuItem;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

public class MainActivity extends SlidingFragmentActivity{
	private SlidingMenu slidingMenu;
	private MenuEsquerdoFragment menuEsquerdoFragment;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu_direito_fake);
		createSlidingMenu();
		createMenu(savedInstanceState);
		createInicio(savedInstanceState);
	}
	
	private void createMenu(Bundle savedInstanceState){
		setBehindContentView(R.layout.menu_esquerdo_fake);
		if(savedInstanceState == null){
			menuEsquerdoFragment = new MenuEsquerdoFragment();
			menuEsquerdoFragment.setRetainInstance(true);
			getSupportFragmentManager().beginTransaction().replace(R.id.menu_esquerdo, menuEsquerdoFragment).commit();
		} else {
			menuEsquerdoFragment = (MenuEsquerdoFragment) getSupportFragmentManager().findFragmentById(R.id.menu_esquerdo);
		}
	}
	
	private void createInicio(Bundle savedInstanceState){
		if(savedInstanceState == null){
			HomeFragment homeFragment = new HomeFragment();
			homeFragment.setRetainInstance(true);
			getSupportFragmentManager().beginTransaction().replace(R.id.menu_direito, homeFragment).commit();
		} else {
			//Just ignore, because the view must be already inflated.
		}
	}

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.main, menu);
//		return true;
//	}

	private void createSlidingMenu(){
		slidingMenu = getSlidingMenu();
		slidingMenu.setMode(SlidingMenu.LEFT);
		slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		slidingMenu.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
        slidingMenu.setShadowDrawable(R.drawable.slidingmenu_shadow);
        slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        slidingMenu.setFadeDegree(0.9f);
        /*slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        slidingMenu.setMenu(R.layout.slidingmenu);*/
    
    	getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}
	
	@Override
	public void onBackPressed() {
		if ( slidingMenu.isMenuShowing()) {
            slidingMenu.toggle();
        }
        else {
            super.onBackPressed();
        }
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ( keyCode == KeyEvent.KEYCODE_MENU ) {
            this.slidingMenu.toggle();
            return true;
        }
        return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
        case android.R.id.home:
            this.slidingMenu.toggle();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }

	}
	
}
